﻿using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Cors;

namespace DTcms.Web.Mvc
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //跨域配置,上线后需要改这个配置，指定域名保证安全
            //http://dtcms.myaspx.wang/
            var corsAttr = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(corsAttr);

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "CustomApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Routes.MapHttpRoute(
                name: "Plugin",
                routeTemplate: "plugin/{controller}/{action}",
                defaults: new { action = "Index", id = RouteParameter.Optional }
            );

            //=>使webapi支持多参数传值
            GlobalConfiguration.Configuration.ParameterBindingRules.Insert(0, SimplePostVariableParameterBinding.HookupParameterBinding);

            //使webapi返回为json
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

            //格式化json数据中的时间的T
            var jsontimeset = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
            jsontimeset.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
            jsontimeset.DateFormatString = "yyyy-MM-dd HH:mm:ss";
            jsontimeset.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
        }
    }
}